import logging
import os
import config


def setup_logger():
    # 获取项目根目录
    project_root = os.path.dirname(os.path.dirname(__file__))

    # 日志文件路径
    log_file = os.path.join(project_root, config.LOG_FILE)

    # 使用basicConfig设置日志的基本配置
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        filename=log_file,
                        filemode='a')
    # 获取默认的logger
    logger = logging.getLogger()
    # 创建一个StreamHandler，用于输出日志到控制台
    console_handler = logging.StreamHandler()
    # 设置StreamHandler的日志级别
    console_handler.setLevel(logging.INFO)
    # 设置StreamHandler的日志格式
    console_formatter = logging.Formatter(
        '%(asctime)s - %(levelname)s - %(message)s')
    console_handler.setFormatter(console_formatter)
    # 将StreamHandler添加到logger中
    logger.addHandler(console_handler)

    # 返回全局的日志记录器
    return logger
